/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "kh_sle_seek.h"
#include "kh_sle_c_interface_test_entry.h"
#include "kh_sle_host.h"
#include "sle_error_code.h"
#include "sle_unit_test_log.h"
#include "stdlib.h"

SleSeekCallbacks *g_sleSeekUtCbs = NULL;

void OnSleUtSeekStateChangeCallback(SleSeekState state)
{
    return;
}

void OnSleUtSeekResultCallback(SleSeekResult *result)
{
    return;
}

SleErrorCode InitKhSleSeekTestEntry(void)
{
    g_sleSeekUtCbs = (SleSeekCallbacks *)malloc(sizeof(SleSeekCallbacks));
    if(g_sleSeekUtCbs == NULL) {
        SLE_UT_LOG("[KhSleSeekTest][FAILED] malloc g_sleSeekUtCbs failed");
        return SLE_ERR_FAILED;
    }
    g_sleSeekUtCbs->OnSleSeekStateChangeCb = OnSleUtSeekStateChangeCallback;
    g_sleSeekUtCbs->OnSleSeekResultCb = OnSleUtSeekResultCallback;
    return SLE_NO_ERROR;
}

SleErrorCode DeinitKhSleSeekTestEntry(void)
{
    free(g_sleSeekUtCbs);
    g_sleSeekUtCbs = NULL;
    return SLE_NO_ERROR;
}

void InitKhSleSeekTestLevel0(void)
{
    SleErrorCode ret = SLE_NO_ERROR;
    ret = InitSleSeekService();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleSeekTest][FAILED]InitKhSleSeekTestLevel0 failed(%d)", ret);
}

void DeinitKhSleSeekTestLevel0(void)
{
    SleErrorCode ret = SLE_NO_ERROR;
    ret = DeinitSleSeekService();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleSeekTest][FAILED]DeinitKhSleSeekTestLevel0 failed(%d)", ret);
}

void RegisterSeekCallbacksTestLevel0(void)
{
    SleErrorCode ret = SLE_NO_ERROR;
    ret = RegisterSeekCallbacks(g_sleSeekUtCbs);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleSeekTest][FAILED]RegisterSeekCallbacksTestLevel0 failed(%d)", ret);
}

void UnRegisterSeekCallbacksTestLevel0(void)
{
    SleErrorCode ret = SLE_NO_ERROR;
    ret = UnregisterSeekCallbacks(g_sleSeekUtCbs);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleSeekTest][FAILED]UnRegisterSeekCallbacksTestLevel0 failed(%d)", ret);
}

void StartSeekTestLevel0(void)
{
    SleErrorCode ret = SLE_NO_ERROR;
    SleSeekParam param = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
    };
    ret = StartSeek(&param);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleSeekTest][FAILED]StartSeekTestLevel0 failed(%d)", ret);
}

void StopSeekTestLevel0(void)
{
    SleErrorCode ret = SLE_NO_ERROR;
    ret = StopSeek();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleSeekTest][FAILED]StopSeekTestLevel0 failed(%d)", ret);
}

void KhSleSeekTestLevel0(void)
{
    SLE_UT_LOG("___KhSleSeekTestLevel0 start");
    InitKhSleSeekTestLevel0();
    RegisterSeekCallbacksTestLevel0();
    StartSeekTestLevel0();
    StopSeekTestLevel0();
    UnRegisterSeekCallbacksTestLevel0();
    DeinitKhSleSeekTestLevel0();
    SLE_UT_LOG("___KhSleSeekTestLevel0 end");
}

void InitKhSleSeekTestLevel1(void)
{
    SleErrorCode ret = SLE_NO_ERROR;
    ret = InitSleSeekService();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleSeekTest][FAILED]InitKhSleSeekTestLevel1 failed1(%d)", ret);

    //检查重复调用
    ret = InitSleSeekService();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleSeekTest][FAILED]InitKhSleSeekTestLevel1 failed2(%d)", ret);
}

void DeinitKhSleSeekTestLevel1(void)
{
    SleErrorCode ret = SLE_NO_ERROR;
    ret = DeinitSleSeekService();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleSeekTest][FAILED]DeinitKhSleSeekTestLevel1 failed1(%d)", ret);

    //检查重复调用
    ret = DeinitSleSeekService();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_ERR_NOT_READY, "[KhSleSeekTest][FAILED]DeinitKhSleSeekTestLevel1 failed2(%d)", ret);
}

void RegisterSeekCallbacksTestLevel1(void)
{
    SleErrorCode ret = SLE_NO_ERROR;
    ret = RegisterSeekCallbacks(NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_ERR_INVALID_PARAM, "[KhSleSeekTest][FAILED]RegisterSeekCallbacksTestLevel1 failed1(%d)", ret);

    ret = RegisterSeekCallbacks(g_sleSeekUtCbs);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleSeekTest][FAILED]RegisterSeekCallbacksTestLevel1 failed2(%d)", ret);

    //检查重复调用
    ret = RegisterSeekCallbacks(g_sleSeekUtCbs);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_ERR_INVALID_PARAM, "[KhSleSeekTest][FAILED]RegisterSeekCallbacksTestLevel1 failed3(%d)", ret);
}

void UnRegisterSeekCallbacksTestLevel1(void)
{
    SleErrorCode ret = SLE_NO_ERROR;
    ret = UnregisterSeekCallbacks(NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_ERR_INVALID_PARAM, "[KhSleSeekTest][FAILED]UnRegisterSeekCallbacksTestLevel1 failed1(%d)", ret);

    ret = UnregisterSeekCallbacks(g_sleSeekUtCbs);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleSeekTest][FAILED]UnRegisterSeekCallbacksTestLevel1 failed2(%d)", ret);

    //检查重复调用
    ret = UnregisterSeekCallbacks(g_sleSeekUtCbs);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_ERR_INVALID_PARAM, "[KhSleSeekTest][FAILED]UnRegisterSeekCallbacksTestLevel1 failed3(%d)", ret);
}

void StartSeekTestLevel1(void)
{
    SleErrorCode ret = SLE_NO_ERROR;
    //测试入参为NULL
    ret = StartSeek(NULL);
    SLE_UT_CHECK_AND_RETURN_LOG(ret == SLE_ERR_INVALID_PARAM, "[KhSleSeekTest][FAILED]StartSeekTestLevel1 failed1(%d)", ret);
    //检查重复调用
    SleSeekParam param = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
    };
    ret = StartSeek(&param);
    SLE_UT_CHECK_AND_RETURN_LOG(ret == SLE_NO_ERROR, "[KhSleSeekTest][FAILED]StartSeekTestLevel1 failed2(%d)", ret);
    ret = StartSeek(&param);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_ERR_EBABLE_SEEK_SUCCESS_WHEN_SEEK, "[KhSleSeekTest][FAILED]StartSeekTestLevel1 failed3(%d)", ret);
}

void StopSeekTestLevel1(void)
{
    SleErrorCode ret = SLE_NO_ERROR;
    ret = StopSeek();
    SLE_UT_CHECK_AND_RETURN_LOG(ret == SLE_NO_ERROR, "[KhSleSeekTest][FAILED]StopSeekTestLevel1 failed1(%d)", ret);
    //检查重复调用
    ret = StopSeek();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_ERR_DISABLE_SEEK_SUCCESS_WHEN_NOT_SEEK, "[KhSleSeekTest][FAILED]StopSeekTestLevel1 failed2(%d)", ret);
}

void KhSleSeekTestLevel1(void)
{
    SLE_UT_LOG("___KhSleSeekTestLevel1 start");
    InitKhSleSeekTestLevel1();
    RegisterSeekCallbacksTestLevel1();
    StartSeekTestLevel1();
    StopSeekTestLevel1();
    UnRegisterSeekCallbacksTestLevel1();
    DeinitKhSleSeekTestLevel1();
    SLE_UT_LOG("___KhSleSeekTestLevel1 end");
}

void KhSleSeekTestEntry()
{
    SLE_UT_LOG("__KhSleSeekTestEntry start");
    SleErrorCode ret = InitKhSleSeekTestEntry();
    SLE_UT_CHECK_AND_RETURN_LOG(ret == SLE_NO_ERROR, "[KhSleSeekTest][FAILED]InitKhSleSleTest failed(%d)", ret);
    KhSleSeekTestLevel0();
    KhSleSeekTestLevel1();
    DeinitKhSleSeekTestEntry();
    SLE_UT_CHECK_AND_RETURN_LOG(ret == SLE_NO_ERROR, "[KhSleSeekTest][FAILED]DeinitKhSleSleTest failed(%d)", ret);
    SLE_UT_LOG("__KhSleSeekTestEntry end");
}